/*
 * Copyright (C) 2015 grandcentrix GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.grandcentrix.tray.sample;

import net.grandcentrix.tray.sample.slice.MainAbilitySlice;
import net.grandcentrix.tray.util.LogUtil;
import net.grandcentrix.tray.core.TrayItem;
import net.grandcentrix.tray.TrayPreferences;
import net.grandcentrix.tray.core.SharedPreferencesImport;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.ArrayList;
import java.util.HashMap;

public class ImportTrayPreferences extends TrayPreferences {
    private static final String TAG = ImportTrayPreferences.class.getSimpleName();

    public static final String KEY_USER_TOKEN = "user_token";
    public static final String KEY_GCM_TOKEN = "gcm_token";

    public ImportTrayPreferences(final Context context) {
        super(context, "imported", 1);
    }

    @Override
    protected void onCreate(final int initialVersion) {
        super.onCreate(initialVersion);

        // onCreate is only called at the very first creation of this Module
        importSharedPreferencesWithLogging();
    }

    /**
     * example how to import shared preferences
     */
    private void importSharedPreferences() {
        // migrate sharedPreferences in here.
        final SharedPreferencesImport userTokenMigration = new SharedPreferencesImport(getContext(),
                MainAbilitySlice.SHARED_PREF_NAME, "userToken", KEY_USER_TOKEN);
        migrate(userTokenMigration);

        final SharedPreferencesImport gcmTokenMigration = new SharedPreferencesImport(getContext(),
                MainAbilitySlice.SHARED_PREF_NAME, "gcmToken", KEY_GCM_TOKEN);
        migrate(gcmTokenMigration);
    }

    /**
     * logging wrapper for:
     * example how to import shared preferences
     */
    private void importSharedPreferencesWithLogging() {
        DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
        final Preferences sharedPreferences = databaseHelper.getPreferences(MainAbilitySlice.SHARED_PREF_NAME);

        final HashMap<String, ?> allBefore = new HashMap<>(sharedPreferences.getAll());
        LogUtil.info(TAG, allBefore.size() + " items in sharedPreferences: " + allBefore.toString());
        importSharedPreferences();

        final ArrayList<TrayItem> all = new ArrayList<>(getAll());

        LogUtil.info(TAG, "imported " + all.size() + " items: " + all.toString());

        final HashMap<String, ?> allAfter = new HashMap<>(sharedPreferences.getAll());
        LogUtil.info(TAG, allAfter.size() + " items in sharedPreferences: " + allAfter.toString());
    }
}